# /usr/bin/python3
#-*-coding:utf-8-*-
# 运行环境： ubuntu 16.04 LTS/win 10
# tensorflow 1.13.1
# opencv 3.4.1.15
# 数据集： DeepFashion/Category and Attribute Prediction Benchmark
# 查看DeepFashion数据集中的类的样本数, 以及是否做了标定框
import cv2
import matplotlib as mt
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import os
import random
import colorsys
import time
import PIL
import skimage.io as io
import xml.etree.ElementTree as ET

# 这里顺序要放在最后, 不然这个界面显示的GUI不会生效
mt.use('TkAgg');

#  bbox 标定框的地址
bboxes_path = 'winder_yolov3/Category and Attribute Prediction Benchmark/Anno/list_bbox.txt';
# 记录每一张图片属于50种子类别中具体哪一个
list_category_img = 'winder_yolov3/Category and Attribute Prediction Benchmark/Anno/list_category_img.txt';
# 记录并把50种子类归纳成三大类：1、2、3, 1和3 是外套和连体衣, 2是裤子
list_category_cloth = 'winder_yolov3/Category and Attribute Prediction Benchmark/Anno/list_category_cloth.txt';
# 图片所在路径的前置目录
image_dir = 'Category and Attribute Prediction Benchmark/Img/img/';
# 用来存储50种子类别的列表
winder_image_class = [];
with open(list_category_img, 'r', encoding='utf-8') as r:
    winder_category_img = r.readlines();
    num = 0;
    for data in winder_category_img:
        if len(data.split()[1:]) == 0:
            continue;
        num += 1;
        if num < 2:
            continue
        winder_image_class.append(data.split());
    print(len(winder_image_class));
    print(winder_image_class[0][0]);
    print(winder_image_class[0][1]);

# 用来存储三大类的列表
winder_image_cloth = [];
with open(list_category_cloth, 'r', encoding='utf-8') as r:
    winder_category_cloth = r.readlines();
    num = 0;
    for data_1 in winder_category_cloth:
        if len(data_1.split()[1:]) == 0:
            continue;
        num += 1;
        if num < 2:
            continue;
        winder_image_cloth.append(data_1.split());
    print(len(winder_image_cloth));
    print(winder_image_cloth[0][0]);
    print(winder_image_cloth[0][1]);

# 用来存储标定框的列表
winder_bboxes_list = [];
with open(bboxes_path, 'r', encoding='utf-8') as r:
    bboxes_list = r.readlines();
    num = 0;
    for box in bboxes_list:
        if len(box.split()[1:]) == 0:
            continue;
        num += 1;
        if num < 2:
            continue;
        winder_bboxes_list.append(box.split());
    print(len(winder_bboxes_list));
    print(winder_bboxes_list[0][0]);
    print(winder_bboxes_list[0][1:]);

# 开始创建文件, 用于把所有的图片路径以及标定框写入TXT 文本中;
# 用于存放整理出来的数据集的文本的文件夹名字;
datasets_folder = 'winder_yolov3/winder_datasets';
if not os.path.exists(datasets_folder):
    os.makedirs(datasets_folder);
train_dataset = os.path.join(datasets_folder, 'train of winder');
print(train_dataset); # winder_datasets/train of winder
# 对于一个即将要写入数据集信息的文本, 命名一个新的名字
datasets_file_name = 'winder_fashion_dataset';
#label_file_name = 'winder_label';
type_datasets = 'txt';

# 训练集文本的路径
train_dataset_path = train_dataset + '/' + datasets_file_name + '.' + type_datasets;
print('训练数据集的文本的路径：', train_dataset_path);
if not os.path.exists(train_dataset_path):
    #os.makedirs(train_dataset_path);
    f = open(train_dataset_path, 'w');
    f.close();

with open(train_dataset_path, 'a', encoding='utf-8') as f:
    count = 0;# 计数器, 记录一共存储的数据总数
    for index in range(len(winder_bboxes_list)):
        image_path = image_dir + winder_bboxes_list[index][0];
        xmin, ymin, xmax, ymax = winder_bboxes_list[index][1:]
        category_ID = winder_image_class[index][1];
        category_ID = int(category_ID);
        if category_ID < 1 or category_ID > 50:
            print('50种子类的序列号只在1~50范围内, 此ID号超出索引！');
            continue;
        # 找到每一张图片在三大类中的类别标签具体归属
        winder_class_data = winder_image_cloth[category_ID - 1];
        temp_class_id = winder_class_data[1];
        if int(temp_class_id) < 1 or int(temp_class_id) > 3:
            print('三大类的ID号只有1、2、3, 此ID号超出索引！');
            continue;
        if int(temp_class_id) == 2:
            # 标签为2, 表示是裤子
            winder_label_class = 48;
        else:
            winder_label_class = 49; # 如果是1、3, 那就是表示为外套和连体衣;
        # 具体子类别名称
        winder_Subcategories = winder_class_data[0];
        text_input = image_path + ' ' + ','.join([str(xmin), str(ymin), str(xmax), str(ymax), str(winder_label_class)]) \
                     + ' ' + winder_Subcategories;
        f.write(text_input + '\n');
        count += 1;
    print('数据写入的总数： ', count);

# LOG:
'''
289222
img/Sheer_Pleated-Front_Blouse/img_00000001.jpg
3
50
Anorak
1
289222
img/Sheer_Pleated-Front_Blouse/img_00000001.jpg
['072', '079', '232', '273']
winder_yolov3/winder_datasets\train of winder
训练数据集的文本的路径： winder_yolov3/winder_datasets\train of winder/winder_fashion_dataset.txt
数据写入的总数：  289222
'''
